package com.hellobike.haxing.modules.product.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.common.constant.CacheConstant;
import com.hellobike.haxing.common.vo.ProductResultCode;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.product.entity.SkuWarehouseStock;
import com.hellobike.haxing.modules.product.entity.WmsWarehouseShop;
import com.hellobike.haxing.modules.product.exception.ProductException;
import com.hellobike.haxing.modules.product.mapper.SkuWarehouseStockMapper;
import com.hellobike.haxing.modules.product.mapper.WmsStockSyncMapper;
import com.hellobike.haxing.modules.product.mapper.WmsWarehouseShopMapper;
import com.hellobike.haxing.modules.product.service.ISkuWarehouseStockService;
import com.hellobike.haxing.common.util.RedisCacheLoader;
import com.hellobike.haxing.common.util.RedisCacheUtil;
import com.hellobike.haxing.modules.supplier.consumer.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Description
 * @Author ZLB
 * @Date 2020/2/17
 * @Version 1.0
 **/
@Service
public class SkuWarehouseStockServiceImpl extends ServiceImpl<SkuWarehouseStockMapper, SkuWarehouseStock> implements ISkuWarehouseStockService {

    @Autowired
    private RedisCacheUtil redisCacheUtil;
    @Autowired
    private SkuWarehouseStockMapper skuWarehouseStockMapper;
    @Autowired
    private WmsWarehouseShopMapper wmsWarehouseShopMapper;
    @Autowired
    private WmsStockSyncMapper wmsStockSyncMapper;

    @Override
    public Result changeSkuStocks(Map<Long, Integer> map, Long shopId) {
        Long warehouseId = getShopBindingWarehouseId(shopId);
        if (warehouseId == null) {
            return Result.error(ProductResultCode.STOCK_NOT_ENOUGH, "库存不足");
        }
        try {
            //必须通过代理调用，使事务生效
            for (Map.Entry<Long, Integer> entry : map.entrySet()) {
                Long skuId = entry.getKey();
                Integer changeNum = entry.getValue();
                int flag;
                if (changeNum < 0) {
                    flag = skuWarehouseStockMapper.changeSkuWarehouseStock(skuId, warehouseId, changeNum);
                } else {
                    flag = skuWarehouseStockMapper.changeSkuWarehouseStockForce(skuId, warehouseId, changeNum);
                }
                if (flag == 0) {
                    throw new ProductException("库存不足");
                }
            }
            return Result.ok();
        } catch (ProductException e) {
            return Result.error(ProductResultCode.STOCK_NOT_ENOUGH, "库存不足");
        }
    }

    /**
     * 获取站点所对应的仓库ID
     *
     * @param shopId
     * @return
     */
    private Long getShopBindingWarehouseId(Long shopId) {
        Integer status = wmsStockSyncMapper.selectShopStatus(shopId);
        /**
         * 如果店铺关闭，直接返回不存在的仓库
         */
        if (!Integer.valueOf(1).equals(status)) {
            return -1L;
        }
        return redisCacheUtil.get(CacheConstant.SHOP_BINDING_WAREHOUSE, shopId, new TypeReference<Long>() {
        }, Constants.ONE_HOUR, new RedisCacheLoader<Long, Long>() {
            @Override
            public Long load(Long loadId) {
                QueryWrapper<WmsWarehouseShop> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(WmsWarehouseShop::getShopId, shopId);
                WmsWarehouseShop wmsWarehouseShop = wmsWarehouseShopMapper.selectOne(queryWrapper);
                return wmsWarehouseShop == null ? null : wmsWarehouseShop.getWarehouseId();
            }
        });
    }
}
